package DengLu;

import GJ.ArrayList;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class Register extends JFrame {

    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JPanel panel1;
    private boolean flag1,flag2;
    private JButton button1;
    private JTextField textField1;
    private JTextField textField2;


    public static void main(String args[]){
        Register register = new Register();
        register.setVisible(true);
    }

    public Register(){
        super();
        setTitle("注册界面");
        setBounds(350,100,1200,800);
        getContentPane().setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel1 = new JPanel();
        panel1.setBackground(Color.orange);
        panel1.setBounds(0,0,1200,800);
        panel1.setLayout(null);
        getContentPane().add(panel1);

        label1 = new JLabel("欢迎您的加入");
        label1.setBounds(550,80,300,100);
        label1.setFont(new Font("",Font.BOLD,40));
        panel1.add(label1);

        label2 = new JLabel("账号：");
        label2.setBounds(180 ,200,450,50);
        label2.setFont(new Font("",Font.BOLD,28));
        label2.setHorizontalAlignment(JLabel.CENTER);
        panel1.add(label2);

        label3 = new JLabel("密码：");
        label3.setBounds(180,300,450,50);
        label3.setFont(new Font("",Font.BOLD,28));
        label3.setHorizontalAlignment(JLabel.CENTER);
        panel1.add(label3);

        textField1 = new JTextField(5);
        textField1.setBounds(450,200,500,50);
        textField1.setFont(new Font("",Font.BOLD,22));
        textField1.setBackground(Color.orange);
        panel1.add(textField1);
        textField2 = new JPasswordField(5);
        textField2.setBounds(450,300,500,50);
        textField2.setFont(new Font("",Font.BOLD,22));
        textField2.setBackground(Color.orange);
        panel1.add(textField2);

        button1 = new JButton("注册");
        button1.setBounds(650 ,400,100,50);
        button1.setFont(new Font("",Font.BOLD,22));
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str1 ="账号："+textField1.getText()+";";
                String str2 ="密码："+textField2.getText()+";";
                String str3 = textField1.getText();
                String str4 = textField2.getText();
                File file = new File("D://Account");
                ArrayList arrayList = new ArrayList(100);
                BufferedReader bufferedReader = null;
                try {
                    bufferedReader = new BufferedReader(new FileReader(file));
                    String line;
                    String[] song;
                    while ((line=bufferedReader.readLine())!=null){
                        song = line.split(";");
                        for (int i=0;i<song.length;i++){
                            arrayList.addBooks(song[i]);
                        }
                        if (arrayList.get(0).equals(str3)){
                            flag1 = true;
                            break;
                        }else{
                            flag1 = false;
                            break;
                        }
                    }
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

                if (flag1){
                    JOptionPane.showMessageDialog(null,"账号或者密码相同，请重新输入！");
                    dispose();
                    new Register().setVisible(true);
                }else{
                    File file1 = new File("D://Account");
                    BufferedWriter bufferedWriter = null;
                    try {
                        bufferedWriter = new BufferedWriter(new FileWriter(file1,true));
                        arrayList.clear();
                        arrayList.addBooks(str1);
                        arrayList.addBooks(str2);
                        for (int i=0;i<arrayList.size;i++){
                            bufferedWriter.write(arrayList.get(i));
                            bufferedWriter.flush();
                        }
                        bufferedWriter.newLine();
                        flag2 = true;
                        bufferedWriter.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    if (flag2){
                        JOptionPane.showMessageDialog(null,"注册成功！");
                        dispose();
                        new Entry().setVisible(true);
                    }
                }

            }
        });
        panel1.add(button1);


    }


}




































